********************************************************************************
** 	TITLE: ROC curve 
**
**	PURPOSE: Appendix Figure 1
**							
**	INPUTS: "$adta/Wide with outcomes.dta"
**	
**	OUTPUTS: "$gph/Combined ROC.png"
**				
**	NOTES: 
**
**	AUTHOR: 
**
** 	MODIFIED BY: Hasan Ahamed
**
**	CREATED: 3/8/2018- moved to analysis from exploratory
**
**	EDITED: 3/1/2023
********************************************************************************
clear all
version 14.2
pause on
set more off
capture log close

use "$adta/Wide with outcomes.dta", clear
graph set window fontface "Times New Roman"

keep if !mi(enc)

*preparing individual figures
foreach restrict in flag_randomized ltca htca {
	preserve 
	keep if `restrict' == 1
	roctab 	binindex18 negficoscore086 if enc == 0
	local auccont = string(r(area), "%14.2f")
	roctab 	binindex18 negficoscore086 if enc == 1
	local auctreat = string(r(area), "%14.2f")
	roccomp binindex18 negficoscore086, by(enc)
	local pvalue = string(r(p), "%14.2f")

**Top row
	if "`restrict'" == "flag_randomized" {
		#delimit ;
		roccomp binindex18 negficoscore086, by(enc) graph
		b1title("False positive rate (1-specificity)", size(large)) 
		b2title("Extra Step: `auccont', CBL: `auctreat', p-value: `pvalue'.", size(large))
		title("Full Sample", size(huge)) 
		ytitle("") 
		l2title("True positive rate (sensitivity)", size(large)) 
		xtitle(" ")
		xlabel(0(.25)1) 
		ylabel(0(.25)1)
		scheme(s1mono)
		legend(off)
		nodraw;
		#delimit cr ;
		graph rename g1
		graph save "$gph/binindex18_`restrict'.gph", replace
	}
	else if "`restrict'" == "ltca" {
		#delimit ;
		roccomp binindex18 negficoscore086, by(enc) graph
		b1title("False positive rate (1-specificity)", size(large)) 
		b2title("Extra Step: `auccont', CBL: `auctreat', p-value: `pvalue'.", size(large))
		title("1 = Lowest tercile of baseline installment activity", size(huge)) 
		l2title("True positive rate (sensitivity)", size(large)) 
		xtitle(" ")
		ytitle(" ")
		xlabel(0(.25)1) 
		ylabel(0(.25)1)
		legend(off)
		scheme(s1mono)
		nodraw;
		#delimit cr ;	
		graph rename g2
		graph save "$gph/binindex18_`restrict'.gph", replace	
	}
	else if "`restrict'" == "htca" {
		#delimit ;
		roccomp binindex18 negficoscore086, by(enc) graph
		b1title("False positive rate (1-specificity)", size(large)) 
		b2title("Extra Step: `auccont', CBL: `auctreat', p-value: `pvalue'.", size(large))
		title("1 = Highest tercile of baseline installment activity", size(huge)) 
		l2title("True positive rate (sensitivity)", size(large)) 
		xtitle(" ")
		ytitle(" ")
		xlabel(0(.25)1) 
		ylabel(0(.25)1)
		legend(label(1 "Extra Step") label(2 "CBL") label(3 "Reference") position(5) bmargin(0 2 10 0)) 
		scheme(s1mono)
		nodraw;
		#delimit cr ;
		graph rename g3
		graph save "$gph/binindex18_`restrict'.gph", replace
	}
	sleep 1000
	restore
}


cap erase "$gph/Combined ROC.gph"
				
#delimit ;

*combinning all the graphs
grc1leg "$gph/binindex18_flag_randomized.gph" "$gph/binindex18_ltca.gph" "$gph/binindex18_htca.gph" , 
				altshrink 
				saving("$gph/Combined ROC.gph") 
				title("Appendix Figure 1. Do CBLs change credit scores' predictive power?") 
				subtitle("Testing for differences in fit using Area Under the Curve (AUC) comparisons", span) 
				scheme(s1mono) ycommon
				iscale(1)
				legendfrom("$gph/binindex18_htca.gph") 
				position(5) ring(0)
				imargin(medsmall);
#delimit cr ;



graph save "$gph/Combined ROC.gph", replace
graph export "$gph/Combined ROC.png", replace as(png)


*EOF*














